package ee.emhi;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class TabHost extends TabActivity {
	/** Called when the activity is first created. */
	static android.widget.TabHost tabHost;
	static ImageView icon_hoiatused;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_host);

		tabHost = getTabHost();

		// tab for Linnad
		TabSpec tabLinnad = tabHost.newTabSpec("Linnad");
		View linnadIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView tvLinnad = (TextView) linnadIndicator.findViewById(R.id.title);
		ImageView icon_linnad = (ImageView) linnadIndicator
				.findViewById(R.id.icon);
		icon_linnad.setImageResource(R.drawable.tab_linnad);
		tvLinnad.setText("Linnad");
		tabLinnad.setIndicator(linnadIndicator);
		try {
			Class<?> linnad = Class.forName("ee.emhi.Linnad");
			Intent iLinnad = new Intent(TabHost.this, linnad);
			tabLinnad.setContent(iLinnad);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		// tab for Prognoos
		TabSpec tabPrognoos = tabHost.newTabSpec("Prognoos");
		View prognoosIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView tvPrognoos = (TextView) prognoosIndicator
				.findViewById(R.id.title);
		ImageView icon_prognoos = (ImageView) prognoosIndicator
				.findViewById(R.id.icon);
		icon_prognoos.setImageResource(R.drawable.tab_prognoos);
		tvPrognoos.setText("Prognoos");
		tabPrognoos.setIndicator(prognoosIndicator);
		try {
			Class<?> prognoos = Class.forName("ee.emhi.Prognoos");
			Intent iPrognoos = new Intent(this, prognoos);
			tabPrognoos.setContent(iPrognoos);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		// tab for Satelliidid
		TabSpec tabSatelliidid = tabHost.newTabSpec("Satelliidid");
		View satelliididIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView tvSatelliidid = (TextView) satelliididIndicator
				.findViewById(R.id.title);
		ImageView icon_satelliidid = (ImageView) satelliididIndicator
				.findViewById(R.id.icon);
		icon_satelliidid.setImageResource(R.drawable.tab_satelliidid);
		tvSatelliidid.setText("Satelliidid");
		tabSatelliidid.setIndicator(satelliididIndicator);
		try {
			Class<?> satelliidid = Class.forName("ee.emhi.Satelliidid");
			Intent iSatelliidid = new Intent(this, satelliidid);
			tabSatelliidid.setContent(iSatelliidid);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		// tab for Hoiatused
		TabSpec tabHoiatused = tabHost.newTabSpec("Hoiatused");
		View hoiatusedIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView tvHoiatused = (TextView) hoiatusedIndicator
				.findViewById(R.id.title);
		icon_hoiatused = (ImageView) hoiatusedIndicator.findViewById(R.id.icon);
		if (Splashscreen.warningsCheck()) {
			hasWarnings();
		} else {
			noWarnings();
		}
		tvHoiatused.setText("Hoiatused");
		tabHoiatused.setIndicator(hoiatusedIndicator);
		try {
			Class<?> hoiatused = Class.forName("ee.emhi.Hoiatused");
			Intent iHoiatused = new Intent(TabHost.this, hoiatused);
			tabHoiatused.setContent(iHoiatused);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		// tab for Meteogrammid
		TabSpec tabMeteo = tabHost.newTabSpec("Meteogrammid");
		View meteoIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView tvMeteo = (TextView) meteoIndicator.findViewById(R.id.title);
		ImageView icon_meteo = (ImageView) meteoIndicator
				.findViewById(R.id.icon);
		icon_meteo.setImageResource(R.drawable.tab_meteo);
		tvMeteo.setText("Graafikud");
		tabMeteo.setIndicator(meteoIndicator);
		try {
			Class<?> meteogrammid = Class.forName("ee.emhi.Meteogrammid");
			Intent iMeteo = new Intent(TabHost.this, meteogrammid);
			tabMeteo.setContent(iMeteo);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		// Adding all TabSpec to TabHost
		tabHost.addTab(tabLinnad); // Adding Linnad tab
		tabHost.addTab(tabPrognoos); // Adding Prognoos tab
		tabHost.addTab(tabSatelliidid); // Adding Satelliidid tab
		tabHost.addTab(tabHoiatused); // Adding Hoiatused tab
		tabHost.addTab(tabMeteo); // Adding Meteogrammid tab

		for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
			tabHost.getTabWidget().getChildAt(i)
					.setBackgroundColor(Color.TRANSPARENT);
			tabHost.setBackgroundResource(R.drawable.background);
		}
	}

	public void hasWarnings() {
		icon_hoiatused.setImageResource(R.drawable.tab_hoiatused_alert);
	}

	public void noWarnings() {
		icon_hoiatused.setImageResource(R.drawable.tab_hoiatused);
	}
	
	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInf = getMenuInflater();
		menuInf.inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.aboutUs:
			Intent i = new Intent("ee.emhi.ABOUT");
			startActivity(i);
			break;
		case R.id.exit:
			finish();
			break;
		}
		return false;
	}
}